package cn.tongji.edu.tjmobile;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class WelcomeActivity extends Activity implements GestureDetector.OnGestureListener {

    private GestureDetector detector = new GestureDetector(this);
    private ViewFlipper flipper;
    private static int VIEW_NUMBER = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences settings = getSharedPreferences("setting", 0);
        boolean isFirst = settings.getBoolean("isFirst", true);

        if(isFirst){
            SharedPreferences.Editor editor = settings.edit();
            editor.putBoolean("isFirst", false);
            editor.commit();
        }else{
            String userId = settings.getString("userId", "");
            if(userId.equals("")) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.setClass(WelcomeActivity.this, LoginActivity.class);
                startActivity(intent);
            }else{
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                intent.setClass(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
            }
        }

        setContentView(R.layout.activity_welcome);
        FrameLayout welcomeLayout = (FrameLayout) findViewById(R.id.welcome_layout);
        welcomeLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

        flipper = (ViewFlipper) findViewById(R.id.welcome_view_flipper);
        flipper.addView(addImageView(R.drawable.welcome_slide_1));
        flipper.addView(addImageView(R.drawable.welcome_slide_2));
        flipper.addView(addImageView(R.drawable.welcome_slide_3));
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return this.detector.onTouchEvent(event);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    private View addImageView(int id) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(id);
        return iv;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        View currentView = this.flipper.getCurrentView();
        int index = this.flipper.indexOfChild(currentView);

        if (e1.getX() - e2.getX() > 120) {
            if (index != VIEW_NUMBER - 1) {
                this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_right_in));
                this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                        R.anim.animation_left_out));
                this.flipper.showNext();
            } else {
                Intent intent = new Intent();
                intent.setClass(WelcomeActivity.this, LoginActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.animation_right_in, R.anim.animation_left_out);
            }
            return true;
        } else if (e1.getX() - e2.getX() < -120 && index > 0) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.animation_left_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                    R.anim.animation_right_out));
            this.flipper.showPrevious();
            return true;
        }

        return false;
    }
}